package com.hqyj.lyf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.lyf.mapper.UserMapper;
import com.hqyj.lyf.pojo.User;
import com.hqyj.lyf.service.UserService;
import com.hqyj.lyf.utils.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * User 控制器
 */
@RestController
@RequestMapping("/user")
public class UserController {

    // 注入UserService接口
    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    // login
    @PostMapping("/login")
    public ResultInfo login(User user){
        return userService.login(user);
    }


    // 分页查询
    @RequestMapping("/kSList")
    public Map<String,Object> showUser(@RequestParam(required = false,defaultValue = "1") int page,
                                       @RequestParam(required = false) int limit){
        //执行分页查询
        Page<User> page1 = new Page<>(page,limit); //参数就是页码、每页条数
        //封装数据
        Page<User> userPage = userMapper.selectPage(page1, new QueryWrapper<User>().eq("user_role_id",2));
        Map<String, Object> map = new HashMap<>();
        //需要返回一个code，且值必须为0
        map.put("code",0);
        map.put("msg","查询成功");
        map.put("data",userPage.getRecords()); //当前页码数据内容
        map.put("count",userPage.getTotal()); //总条数

        return map;
    }


    // // 删除单条考生数据
    @PostMapping("/examineeDelete")
    public ResultInfo examineeDelete(User user){
        return userService.examineeDeleteByUserId(user);
    }

    // 修改单条考生数据
    @PostMapping("/examineeUpdate")
    public ResultInfo examineeUpdate(User user){
        return userService.examineeUpdate(user);
    }

    // 添加单条考生数据
    @PostMapping("/examineeAdd")
    public ResultInfo examineeAdd(User user){
        System.out.println("=========================="+user);
        return userService.examineeAdd(user);
    }

    // 批量删除
    @PostMapping("/examineeBatchDel")
    public ResultInfo examineeBatchDel(String arr){
        System.out.println("-------------------------"+arr);
        // 分割字符串
        String[] split = arr.split(",");
        int[] intArr = new int[split.length];
        // 将字符串数组转为int数组
        for (int i = 0; i < split.length; i++) {
            int parseInt = Integer.parseInt(split[i]);
            intArr[i] = parseInt;
        }
        return userService.examineeBatchDel(intArr);
    }

}
